package handler

import (
	"encoding/json"
	"holiday-work/cache"
	"holiday-work/model"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// 商品添加

func ShopAdd(c *gin.Context) {
	shop_code := c.PostForm("shop_code")
	shop_name := c.PostForm("shop_name")

	shop := model.Shop{
		ShopCode: shop_code,
		ShopName: shop_name,
	}
	err := model.DB.Create(&shop).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 商品订单添加

func OrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	sid, _ := strconv.Atoi(c.PostForm("sid"))

	order := model.Order{
		OrderCode: order_code,
		OrderName: order_name,
		Sid:       sid,
	}
	err := model.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

// 商品订单列表

func OrderList(c *gin.Context) {
	/*
		select
		orders.id as OrderId,
		orders.order_code as OrderCode,
		orders.order_name as OrderName,
		shops.shop_code as ShopCode,
		shops.shop_name as ShopName
		from orders
		left join shops on shops.id = orders.sid
	*/
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var shopOrderList []model.ShopOrderList

	redis_key := "order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"shops.shop_code as ShopCode",
				"shops.shop_name as ShopName",
			).
			Joins("left join shops on shops.id = orders.sid").
			Offset(offset).Limit(size).Debug().Find(&shopOrderList)
		jsonData, _ := json.Marshal(shopOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &shopOrderList)
	}

	var total int64
	model.DB.Model(&model.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表获取成功",
		"data":  shopOrderList,
		"total": total,
	})
	return
}

// 商品订单详情

func OrderSelect(c *gin.Context) {
	/*
			select
			orders.id as OrderId,
			orders.order_code as OrderCode,
			orders.order_name as OrderName,
			shops.shop_code as ShopCode,
			shops.shop_name as ShopName
			from orders
			left join shops on shops.id = orders.sid
		    where orders.id = 1
	*/

	id := c.Query("id")

	var shopOrderList model.ShopOrderList

	redis_key := "order_select_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"shops.shop_code as ShopCode",
				"shops.shop_name as ShopName",
			).
			Joins("left join shops on shops.id = orders.sid").
			Where("orders.id = ?", id).
			Debug().Find(&shopOrderList)
		jsonData, _ := json.Marshal(shopOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &shopOrderList)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "搜索成功",
		"data": shopOrderList,
	})
	return
}
